<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>构建父-子文档索引 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexing-parent-child.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexing-parent-child.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/indexing-parent-child.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/indexing-parent-child.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">构建父-子文档索引</span>
</div>
<div class="navheader">
<span class="prev">
<a href="parent-child-mapping.html">« 父-子关系文档映射</a>
</span>
<span class="next">
<a href="has-child.html">通过子文档查询父文档 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="indexing-parent-child"></a>构建父-子文档索引<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/45_Indexing_parent_child.asciidoc">edit</a>
</h2>
</div></div></div>
<p>为父文档创建索引与为普通文档创建索引没有区别。父文档并不需要知道它有哪些子文档。</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">POST /company/branch/_bulk
{ "index": { "_id": "london" }}
{ "name": "London Westminster", "city": "London", "country": "UK" }
{ "index": { "_id": "liverpool" }}
{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }
{ "index": { "_id": "paris" }}
{ "name": "Champs Élysées", "city": "Paris", "country": "France" }</pre>
</div>
<p>创建子文档时，用户必须要通过 <code class="literal">parent</code> 参数来指定该子文档的父文档 ID：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /company/employee/1?parent=london <a id="CO279-1"></a><i class="conum" data-value="1"></i>
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO279-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>当前<code class="word">employee</code>文档的父文档 ID 是<code class="word">london</code>。</p>
</td>
</tr>
</table>
</div>
<p>父文档<code class="literal">parent</code>的 ID 有两个作用：1). 创建父文档和子文档之间的关系，2). 保证父文档和子文档都在同一个分片上。</p>

<p>在 <a class="xref" href="routing-value.html" title="路由一个文档到一个分片">路由一个文档到一个分片</a> 中，我们解释了 Elasticsearch 如何通过路由值来决定该文档属于哪一个分片，路由值默认为该文档的 <code class="literal">_id</code> 。分片路由的计算公式如下：</p>

<pre class="literallayout">shard = hash(routing) % number_of_primary_shards</pre>

<p>如果指定了<code class="literal">parent</code>ID，那么就会使用父文档的 ID 进行路由，而不会使用当前文档<code class="literal">_id</code>。也就是说，如果父文档和子文档都使用相同的值进行路由，那么父文档和子文档都会分布在同一个分片上。</p>

<p>在执行单文档的请求时需要指定<code class="literal">parent</code>ID，单文档请求包括：通过 <code class="literal">GET</code> 请求获取一个子文档；创建、更新或删除一个子文档。
与转发到索引中的所有分片的搜索请求不同，这些单文档请求只转发到保存文档的分片 —— 如果没有指定<code class="literal">parent</code>ID，请求可能会被转发到错误的分片。
</p>
<p><code class="literal">parent</code>ID 应该在<code class="literal">bulk</code>API中指定：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">POST /company/employee/_bulk
{ "index": { "_id": 2, "parent": "london" }}
{ "name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving" }
{ "index": { "_id": 3, "parent": "liverpool" }}
{ "name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking" }
{ "index": { "_id": 4, "parent": "paris" }}
{ "name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses" }</pre>
</div>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>如果你想要改变一个子文档的 <code class="literal">parent</code> 值，仅通过更新这个子文档是不够的，因为新的父文档有可能在另外一个分片上。因此，你必须要先把子文档删除，然后再重新索引这个子文档。</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="parent-child-mapping.html">« 父-子关系文档映射</a>
</span>
<span class="next">
<a href="has-child.html">通过子文档查询父文档 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>